package com.boc.langxiang.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.util.StringUtil;

/**
 * Created by Li Guoxi on 2016/7/8.
 */
public class SearchEditor extends RelativeLayout {
    ImageView searchIconIv;
    EditText searchEdt;
    ImageView searchCloseIv;
    RelativeLayout searchEditorLayout;
    RelativeLayout iconLayout;
    private Context mContext;

    private boolean isOpen = false;

    private boolean isCloseVisible = true;
    private OnSearchListener mOnSearchListener;

    public SearchEditor(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public SearchEditor(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SearchEditor);
        isOpen = typedArray.getBoolean(R.styleable.SearchEditor_isOpen, false);
        isCloseVisible = typedArray.getBoolean(R.styleable.SearchEditor_isCloseVisible, true);
        init();
    }

    public SearchEditor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.search_editor, this);

        searchEditorLayout = (RelativeLayout) findViewById(R.id.search_editor_laoyut);
        searchEdt = (EditText) findViewById(R.id.search_edt);
        searchCloseIv = (ImageView) findViewById(R.id.search_close_iv);
        searchIconIv = (ImageView) findViewById(R.id.search_icon_iv);
        iconLayout = (RelativeLayout) findViewById(R.id.icon_layout);


        iconLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setOpen(true);
                setSearchOpen();
            }
        });

        searchCloseIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setOpen(false);
                setSearchOpen();
            }
        });

        searchEdt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    if (!StringUtil.isEmpty(searchEdt.getText().toString())) {
                        if (mOnSearchListener != null) {
                            mOnSearchListener.onSearch(searchEdt.getText().toString());
                        }
                    }
                }
                return false;
            }
        });

        setSearchOpen();
        setCloseVisible();
    }

    private void setCloseVisible() {
        if (isCloseVisible) {
            searchCloseIv.setVisibility(VISIBLE);
        } else {
            searchCloseIv.setVisibility(GONE);
        }
    }

    private void setSearchOpen() {
        if (isOpen) {
            iconLayout.setVisibility(GONE);
            searchEditorLayout.setVisibility(VISIBLE);
        } else {
            iconLayout.setVisibility(VISIBLE);
            searchEditorLayout.setVisibility(GONE);
        }
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
        setSearchOpen();
    }

    public boolean isCloseVisible() {
        return isCloseVisible;
    }

    public void setCloseVisible(boolean closeVisible) {
        isCloseVisible = closeVisible;
        setCloseVisible();
    }

    public OnSearchListener getOnSearchListener() {
        return mOnSearchListener;
    }

    public void setOnSearchListener(OnSearchListener onSearchListener) {
        this.mOnSearchListener = onSearchListener;
    }

    public interface OnSearchListener {
        void onSearch(CharSequence searchWord);
    }
}
